package org.pay.service.impl;

import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.pay.core.common.constant.MchConstant;
import org.pay.core.entity.PassageConfig;
import org.pay.core.entity.PassageConfigExample;
import org.pay.core.service.IPassageConfigService;
import org.pay.service.dao.mapper.PassageConfigMapper;

import java.util.List;

/**
 * @author: yangshancheng
 * @date: 18/2/11
 * @description:
 */
@Service
public class PassageConfigServiceImpl implements IPassageConfigService {

    @Autowired
    private PassageConfigMapper passageConfigMapper;

    @Override
    public PassageConfig findById(int id) {
        return passageConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public int add(PassageConfig passageConfig) {
        return passageConfigMapper.insertSelective(passageConfig);
    }

    @Override
    public int update(PassageConfig passageConfig) {
        return passageConfigMapper.updateByPrimaryKeySelective(passageConfig);
    }

    @Override
    public List<PassageConfig> select(int offset, int limit, PassageConfig passageConfig) {
        PassageConfigExample example = new PassageConfigExample();
        example.setOffset(offset);
        example.setLimit(limit);
        PassageConfigExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, passageConfig);
        return passageConfigMapper.selectByExample(example);
    }

    @Override
    public List<PassageConfig> selectAll(PassageConfig passageConfig) {
        PassageConfigExample example = new PassageConfigExample();
        example.setOrderByClause("create_time asc");
        PassageConfigExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, passageConfig);
        return passageConfigMapper.selectByExample(example);
    }

    @Override
    public List<PassageConfig> canAddPassage(List<Integer> values) {
        PassageConfigExample example = new PassageConfigExample();
        example.setOrderByClause("create_time asc");
        PassageConfigExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(MchConstant.PUB_YES);
        if(CollectionUtils.isNotEmpty(values)) criteria.andIdNotIn(values);
        return passageConfigMapper.selectByExample(example);
    }

    @Override
    public Integer count(PassageConfig passageConfig) {
        PassageConfigExample example = new PassageConfigExample();
        PassageConfigExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, passageConfig);
        return passageConfigMapper.countByExample(example);
    }

    void setCriteria(PassageConfigExample.Criteria criteria, PassageConfig passageConfig) {
        if(passageConfig != null) {
            if(passageConfig.getId() != null) criteria.andIdEqualTo(passageConfig.getId());
            if(passageConfig.getState() != null && passageConfig.getState().byteValue() != -99) criteria.andStatusEqualTo(passageConfig.getState());
            if(StringUtils.isNotBlank(passageConfig.getName())) criteria.andNameLike(("%" + passageConfig.getName() + "%"));
        }
    }
}
